// A Set of vetices that can return a random vertice. All vertices are assued to have the same rank.

#ifndef RANKSET_H 
#define RANKSET_H
#include <map>
#include <stdlib.h>
#include "Vertex.h"
#include "VertexDirectory.h"

typedef map<long, Vertex*>::iterator RankSetIterator;
using namespace std;

class Graph;

class RankSet: public VertexContainer
{
	public:
		static const long MinRank = -1;
	
		RankSet(Graph *graph, int rank = 0);
		Vertex* GetRandomVertex(); // Returns vertex and removes it from container
		void InsertNewVertex(VertexBase* vertex);
		void RemoveVertex(Vertex* vertex);
		const long Count();
		const bool IsEmpty();
		const long GetRank() const;
		RankSet *Clone() const;
		RankSet *CloneFromDirectory(Graph *graph, VertexDirectory* dir)  const;
		bool ContainsVertex(Vertex *vertex);
		Graph *GetContainingGraph();
		void InsertAllVerticesToNewGraph(Graph *newGraph);


		bool operator==(const RankSet &other) const; // RankSets are compared by rank
		bool operator<(const RankSet &other) const;
		bool operator>(const RankSet &other) const;
		bool operator!=(const RankSet &other) const;
		bool operator<=(const RankSet &other) const;
		bool operator>=(const RankSet &other) const;


	private:
		map<long, Vertex*> m_VertexSet; // List of vertices, mapped for red-black tree container. Key is index inserted in
		long m_Rank; // The rank of all vertices in container
		long m_MaxIndex; // Running counter for inserting vertices	
		Graph *m_Graph;
		
		int GetMaxIndex();
		void UpdateMaxIndexIfNeeded(int removedIndex);
		RankSetIterator Find(Vertex *vertex);
		void InsertVertex(Vertex *vertex, bool updateNeighbours);
		
};

#endif